package com.dataextend.controller;


import com.alibaba.fastjson.JSONObject;
import com.dataextend.dto.*;
import com.dataextend.entity.UserGroup;
import com.dataextend.entity.UserGroupAnalysisRequest;
import com.dataextend.entity.UserGroupChange;
import com.dataextend.entity.UserGroupRequest;
import com.dataextend.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 *   用户人群分析
 */
@Slf4j
@RestController
@RequestMapping("/v1/UserGroup")
@CrossOrigin(origins={"http://localhost:8080"},allowCredentials="true")
public class UserGroupController {
    @Autowired
    private UserGroupCalculateService userGroupCalculateService;
    @Autowired
    private UserGroupCalculateServiceV2 userGroupCalculateServiceV2;
//    @Autowired
//    private UserGroupAnalysisService userGroupAnalysisService;
    @Autowired
    private UserProfileService userProfileService;
    @Autowired
    private UserGroupService userGroupService;
    @Autowired
    private UserGroupAnalysisServiceV2 userGroupAnalysisServiceV2;


    /**  用户属性筛选菜单 */
    @RequestMapping(value = "/property/list", method = RequestMethod.GET)
    public ResultSegment propertyList() {
        List<PropertyListInfoDTO> resultInfo = userProfileService.propertyList();
        return new ResultSegment(true, 1, "查询成功", resultInfo);
    }


    /**  人群删除 */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public ResultSegment delete(@RequestParam(value = "groupId", required = false) String groupId) {
        userProfileService.delete(groupId);
        return new ResultSegment(true, 1, "查询成功");
    }


    /**  人群分析列表页 */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ResultSegment list(@RequestBody GroupAnalysisListDTO groupAnalysisListDTO) {
        JSONObject obj = userProfileService.list(groupAnalysisListDTO);
        return new ResultSegment(true, 1, "查询成功", obj);
    }


    /**  人群分析 */
    @RequestMapping(value = "/dimension/analysis", method = RequestMethod.GET)
    public ResultSegment dimensionAnalysis(@RequestParam(value = "dimension", required = false) String dimension,
                                           @RequestParam(value = "groupId", required = false) String groupId) {
        List<JSONObject> obj = userGroupAnalysisServiceV2.dimensionAnalysis(dimension, groupId);
        return new ResultSegment(true, 1, "查询成功", obj);
    }


    /**  人群分析维度新增删除 */
    @RequestMapping(value = "/analysis/dimension/editAndadd", method = RequestMethod.GET)
    public ResultSegment analysisDimensionEditAndadd(@RequestParam(value = "dimension", required = false) String dimension,
                                                     @RequestParam(value = "type", required = false) String type) {
        userProfileService.analysisDimensionEditAndadd(dimension, type);
        return new ResultSegment(true, 1, "查询成功");
    }


    /**  人群分析维度列表 */
    @RequestMapping(value = "/analysis/dimension/list", method = RequestMethod.GET)
    public ResultSegment analysisDimensionList() {
        List<String> obj = userProfileService.analysisDimensionList();
        return new ResultSegment(true, 1, "查询成功", obj);
    }


    /**  人群分析下拉菜单 */
    @RequestMapping(value = "/analysis/list", method = RequestMethod.GET)
    public ResultSegment analysisList() {
        List<JSONObject> obj = userProfileService.analysisList();
        return new ResultSegment(true, 1, "查询成功", obj);
    }


    /**  用户属性满足条件筛选 */
    @RequestMapping(value = "/property/filter/list", method = RequestMethod.GET)
    public ResultSegment propertyFilterList() {
        List<JSONObject> obj = userProfileService.propertyFilterList();
        return new ResultSegment(true, 1, "查询成功", obj);
    }


    /** 用户画像 —— 圈人计算获取groupId */
    @RequestMapping(value = "/propertyAndAct/getGroupId", method = RequestMethod.GET)
    public ResultSegment  propertyAndActGetgroupId() {
        String obj = userProfileService.propertyAndActGetgroupId();
        return new ResultSegment(true, 1, "查询成功", obj);
    }


    /** 用户画像 —— 圈人计算 属性 + 行为 */
    @RequestMapping(value = "/propertyAndAct/calculate", method = RequestMethod.POST)
    public ResultSegment propertyAndActCalculate(@RequestBody PropertyAndActDTO userGroupRequest) {
        Integer obj = userGroupCalculateServiceV2.propertyAndActCalculate(userGroupRequest);
        return new ResultSegment(true, 1, "查询成功", obj);
    }


    /** 用户画像 —— 圈人计算 属性 + 行为 条件保存*/
    @RequestMapping(value = "/propertyAndAct/calculate/save", method = RequestMethod.POST)
    public ResultSegment propertyAndActCalculateSave(@RequestBody PropertyAndActDTO userGroupRequest) {
        userGroupCalculateServiceV2.propertyAndActCalculateSave(userGroupRequest);
        return new ResultSegment(true, 1, "查询成功", "");
    }


    /**
     *  用户画像 —— 圈人计算   bingo
     */
    @RequestMapping(value = "/usergroupCalculate", method = RequestMethod.POST)
    public ResultSegment usergroupCalculate(@RequestBody UserGroupRequest userGroupRequest){
        JSONObject userGroupCount = userGroupCalculateService.usergroupCalculate(userGroupRequest);
        return new ResultSegment(true, 1, "查询成功", userGroupCount);
    }


    /**
     *  用户画像 —— 人群多维分析-选择人群
     */
    @RequestMapping(value = "/distinctGroupname", method = RequestMethod.POST)
    public ResultSegment distinctGroupname(){
        List<JSONObject> groupName = userProfileService.distinctGroupname();
        return new ResultSegment(true, 1, "查询成功", groupName);
    }


//    /**
//     *  用户画像 —— 人群多维分析  ok
//     */
//    @RequestMapping(value = "/usergroupAnalysis", method = RequestMethod.POST)
//    public ResultSegment usergroupAnalysis(@RequestBody UserGroupAnalysisRequest userGroupAnalysisRequest){
//        JSONObject usergroupInfo =  userGroupAnalysisService.usergroupCalculate(userGroupAnalysisRequest);
//        return new ResultSegment(true, 1, "查询成功", usergroupInfo);
//    }


    /**
     *  用户画像 —— 人群多维分析 —— 下拉菜单 筛选分析维度
     */
    @RequestMapping(value = "/usergroupAnalysisList", method = RequestMethod.POST)
    public JSONObject usergroupAnalysisList(){
        return userProfileService.usergroupAnalysisList();
    }


    /**
     *  用户画像 —— 人群多维分析 —— 填写标签权重值
     */
    @RequestMapping(value = "/usergroupAnalysisTags", method = RequestMethod.POST)
    public JSONObject usergroupAnalysisTags(@RequestParam(value = "level_2_name", required = false) String level_2_name,
                                            @RequestParam(value = "tag_type", required = false) String tag_type){
        return userProfileService.usergroupAnalysisTags(level_2_name, tag_type);
    }



    /**
     *  用户人群列表 分页展示
     */
    @RequestMapping(value = "/usergroupList", method = RequestMethod.POST)
    public ResultSegment usergroupList(@RequestBody RequestList requestList){
        JSONObject map = userGroupService.listUsergroup(requestList.getPageNo(), requestList.getPageSize());
        return new ResultSegment(true, 1, "查询成功", map);
    }


    /**
     *  新增用户人群
     */
    @RequestMapping(value = "/addUsergroup", method = RequestMethod.POST)
    public JSONObject addUsergroup(@RequestBody UserGroup userGroup){
        return userGroupService.addUsergroup(userGroup);
    }


    /**
     *  删除用户人群
     */
    @RequiresPermissions("usergroup:permission:delete")
    @RequestMapping(value = "/deleteUsergroup", method = RequestMethod.POST)
    public JSONObject deleteUsergroup(@RequestParam(value = "group_id", required = false) String group_id){
        return userGroupService.deleteUsergroup(group_id);
    }


    /**
     *  修改用户人群
     */
    @RequiresPermissions("usergroup:permission:update")
    @RequestMapping(value = "/updateUsergroup", method = RequestMethod.POST)
    public JSONObject updateUsergroup(@RequestBody UserGroupChange userGroupChange){
        return userGroupService.updateUsergroup(userGroupChange);
    }


    /**
     *  根据人群名称进行查询
     */
    @RequestMapping(value = "/searchUsergroup", method = RequestMethod.POST)
    public ResultSegment searchUsergroup(@RequestParam(value = "group_name", required = false) String group_name){
        JSONObject map = userGroupService.searchUsergroup(group_name);
        return new ResultSegment(true, 1, "查询成功", map);
    }


    /**
     *  根据人群ID进行查询
     */
    @RequestMapping(value = "/searchUsergroupById", method = RequestMethod.POST)
    public ResultSegment searchUsergroupById(@RequestParam(value = "group_id", required = false) String group_id){
        JSONObject map = userGroupService.searchUsergroupById(group_id);
        return new ResultSegment(true, 1, "查询成功", map);
    }


    /**
     *  选择人群分析维度
     */
    @RequestMapping(value = "/analysisDimension", method = RequestMethod.POST)
    public ResultSegment analysisDimension(){
        List<String> map = userGroupService.analysisDimension();
        return new ResultSegment(true, 1, "查询成功", map);
    }


    /**
     *  选择人群分析维度-备用 分布分析模块用
     */
    @RequestMapping(value = "/dimensionDistribute", method = RequestMethod.POST)
    public ResultSegment dimensionDistribute(){
        List<String> map = userGroupService.dimensionDistribute();
        return new ResultSegment(true, 1, "查询成功", map);
    }

}
